/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package processor;

import entity.Department;
import entity.Members;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author maynoigi
 */
@Stateless
public class MembersFacade extends AbstractFacade<Members> {
    @PersistenceContext(unitName = "ManTechHelpdesk-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public MembersFacade() {
        super(Members.class);
    }
     public Members checkLogin(String userName, String password) {
        Members members = new Members();
        try {
            members = (Members) em.createQuery("SELECT m FROM Members m WHERE m.membersUser = :User and m.membersPassword = :Password").setParameter("User", userName).setParameter("Password", password).getSingleResult();
            return members;
        } catch (NoResultException ex) {
            return members = null;
        }

    }

    public boolean changePassword(String oldPassword, String newPassword, Integer iD) {
        Members temp = (Members) em.createNamedQuery("Members.findByMembersID").setParameter("membersID", iD).getSingleResult();
        if (temp.getMembersPassword().equals(oldPassword)) {
            temp.setMembersPassword(newPassword);
            edit(temp);
            return true;
        } else {
            return false;
        }
    }

    public Members getMembersByMembersUser(String membersUser) {
        try {
            return (Members) em.createNamedQuery("Members.findByMembersUser").setParameter("membersUser", membersUser).getSingleResult();
        } catch (NoResultException e) {
            System.out.println(e.getMessage());
            return null;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    public List<Members> getTechnicianAll() {
        List<Members> technicians;
        try {
            technicians = em.createQuery("SELECT m FROM Members m WHERE m.membersRole = :role ORDER BY m.membersID DESC").setParameter("role", 1).getResultList();
            return technicians;
        } catch (NoResultException ex) {
            return null;
        }
    }

    public List<Members> getMembersAll(Integer role) {
        List<Members> members = new ArrayList<Members>();
        try {
            if (role == 1) {
                members = em.createQuery("SELECT m FROM Members m WHERE m.membersRole != :role ORDER BY m.membersID DESC").setParameter("role", 2).getResultList();
            }else if(role == 2){
                members = em.createQuery("SELECT m FROM Members m ORDER BY m.membersID DESC").getResultList(); 
            }
            return members;
        } catch (NoResultException ex) {
            return null;
        }
    }

    public Members membersByID(Integer id) {
        Members technicians;
        try {
            technicians = (Members) em.createQuery("SELECT m FROM Members m WHERE m.membersID = :id").setParameter("id", id).getSingleResult();
            return technicians;
        } catch (NoResultException ex) {
            return null;
        }
    }

    public boolean editMembers(Integer membersID, String membersName, String membersUser, String membersGender, String membersAddress, String membersPhone, String membersPassword, String membersPosition, Integer membersRole, Department departmentID) {
        try {
            Members mem = new Members(membersID);
            mem.setMembersName(membersName);
            mem.setMembersUser(membersUser);
            mem.setMembersGender(membersGender);
            mem.setMembersAddress(membersAddress);
            mem.setMembersPhone(membersPhone);
            mem.setMembersPassword(membersPassword);
            mem.setMembersPosition(membersPosition);
            mem.setMembersRole(membersRole);
            mem.setDepartmentID(departmentID);
            em.merge(mem);
            em.flush();
            return true;
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
            return false;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public boolean removeMember(String membersUser) {
        try {
            Members mem = getMembersByMembersUser(membersUser);
            em.remove(mem);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
}
